package com.fenghongzhang.sample.recycleview2

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.fenghongzhang.sample.Food
import com.fenghongzhang.sample.R
import kotlinx.android.synthetic.main.recycleview2_left_layout.view.*
import kotlinx.android.synthetic.main.recycleview2_right_layout.view.*

class MsgRecycleView2Adapter(val data:List<Msg>) : RecyclerView.Adapter<MsgViewHolder>() {


    override fun getItemViewType(position: Int): Int {
        val msg = data[position]
        return msg.type
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MsgViewHolder
    = if(viewType == Msg.SEND){
        val view = LayoutInflater.from(parent.context).inflate(R.layout.recycleview2_left_layout,parent,false)
        println("22222")
        LeftViewHolder(view)
    }else{
        val view = LayoutInflater.from(parent.context).inflate(R.layout.recycleview2_right_layout,parent,false)
        println("11111")

        RightViewHolder(view)
    }

    override fun onBindViewHolder(holder: MsgViewHolder, position: Int) {
        val msg = data[position]
        when(holder){
            is LeftViewHolder -> holder.leftText.text = msg.string
            is RightViewHolder -> holder.rightText.text = msg.string
        }
    }

    override fun getItemCount(): Int = data.size


}